package com.fivefu.core.camera.services.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fivefu.core.camera.entity.DbMonitorRegionCamera;
import com.fivefu.core.camera.mapper.DbMonitorRegionCameraMapper;
import com.fivefu.core.camera.services.DbMonitorCameraService;
import com.fivefu.core.camera.services.DbMonitorRegionCameraService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 组织探头关联表 服务实现类
 * </p>
 *
 * @author 
 * @since 2022-09-27 15:15:29
 */
@Service
public class DbMonitorRegionCameraServiceImpl extends ServiceImpl<DbMonitorRegionCameraMapper, DbMonitorRegionCamera> implements DbMonitorRegionCameraService {

    @Autowired
    private DbMonitorCameraService dbMonitorCameraService;

    /**
     * 编辑组织探头关联关系
     * @param id 组织id
     * @param cameraList 探头id集
     * @param userid 操作用户编号
     */
    @Override
    public void addMonitorRegionCameras(Long id, List<String> cameraList, String userid) {
        cameraList.forEach(cameraId -> {
            //将探头id转为探头编号
            String cameraCode = dbMonitorCameraService.getById(cameraId).getCameraCode();
            //编辑组织探头关联关系
            insertMonitorRegionCamera(id,cameraCode, userid);
        });
    }

    /**
     * 编辑组织探头关联关系
     * @param id 组织id
     * @param cameraCode 探头编号
     * @param userid 操作用户编号
     */
    private void insertMonitorRegionCamera(Long id, String cameraCode, String userid) {
        //判断是否已存在相关的组织探头关联关系
        DbMonitorRegionCamera monitorRegionCamera = queryMonitorRegionCamera(id,cameraCode);
        if (null == monitorRegionCamera) {//无相关组织探头关联关系
            DbMonitorRegionCamera dbMonitorRegionCamera = new DbMonitorRegionCamera();
            LocalDateTime time = LocalDateTime.now();
            dbMonitorRegionCamera.setCreatedBy(userid);
            dbMonitorRegionCamera.setCreatedTime(time);
            dbMonitorRegionCamera.setUpdatedBy(userid);
            dbMonitorRegionCamera.setUpdatedTime(time);
            dbMonitorRegionCamera.setRegionId(id);//组织id
            dbMonitorRegionCamera.setCameraCode(cameraCode);//探头编号
            baseMapper.insert(dbMonitorRegionCamera);
        }
    }

    /**
     * 根据组织id和探头编号查询组织探头关联信息
     * @param id           组织id
     * @param cameraCode   探头编号
     * @return
     */
    private DbMonitorRegionCamera queryMonitorRegionCamera(Long id, String cameraCode) {
        QueryWrapper<DbMonitorRegionCamera> wrapper = new QueryWrapper<>();
        wrapper.eq("is_delete",0);
        wrapper.eq("region_id",id);
        wrapper.eq("camera_code",cameraCode);
        DbMonitorRegionCamera monitorRegionCamera = baseMapper.selectOne(wrapper);
        return monitorRegionCamera;
    }

    /**
     * 获取组织探头关联表所有数据集
     * @return
     */
    @Override
    public List<DbMonitorRegionCamera> queryRegionCameras() {
        QueryWrapper<DbMonitorRegionCamera> wrapper = new QueryWrapper<>();
        wrapper.eq("is_delete",0);
        List<DbMonitorRegionCamera> regionCameraList = baseMapper.selectList(wrapper);
        return regionCameraList;
    }

    /**
     * 移除组织探头关系
     * @param regionId       组织编号
     * @param cameraCode     探头id
     * @param userid         用户编号
     */
    @Override
    public void removeMonitorRegionCamera(Long regionId, String cameraCode, String userid) {
        //根据探头id获取探头编号
        String code = dbMonitorCameraService.getById(cameraCode).getCameraCode();
        DbMonitorRegionCamera regionCamera = queryMonitorRegionCamera(regionId, code);
        LocalDateTime time = LocalDateTime.now();
        regionCamera.setIsDelete(1);
        regionCamera.setUpdatedBy(userid);
        regionCamera.setUpdatedTime(time);
        baseMapper.updateById(regionCamera);
    }

    /**
     * 删除组织探头关联关系
     * @param regionCameraList
     * @param userid   用户编号
     */
    @Override
    public void deleteRegionCamera(List<DbMonitorRegionCamera> regionCameraList,String userid) {
        if (null == regionCameraList || regionCameraList.size() <= 0) {
            return;
        }
        LocalDateTime time = LocalDateTime.now();
        regionCameraList.forEach(dbMonitorRegionCamera -> {
            dbMonitorRegionCamera.setIsDelete(1);
            dbMonitorRegionCamera.setUpdatedBy(userid);
            dbMonitorRegionCamera.setUpdatedTime(time);
        });
        updateBatchById(regionCameraList);
    }

    /**
     * 根据组织编号获取相关联的探头集
     * @param regionId      组织编号
     * @return
     */
    @Override
    public List<DbMonitorRegionCamera> getRegionCameraList(Long regionId) {
        QueryWrapper<DbMonitorRegionCamera> wrapper = new QueryWrapper<>();
        wrapper.eq("is_delete",0);
        wrapper.eq("region_id",regionId);
        List<DbMonitorRegionCamera> regionCameraList = baseMapper.selectList(wrapper);
        return regionCameraList;
    }
}
